package com.example.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.Dto.SetmealDto;
import com.example.domain.Setmeal;
import com.example.domain.SetmealDish;
import com.example.mapper.SetmealMapper;
import com.example.service.ISetmealDishService;
import com.example.service.ISetmealService;
import com.example.service.ex.CountException;
import com.example.service.ex.DeleteException;
import com.example.service.ex.InsertException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 套餐业务
 */
@Service
public class SetmealServiceImpl implements ISetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private ISetmealDishService setmealDishService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public List<Setmeal> getByCategoryId(Long id) {
        //创建条件构造器
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //传递条件
        lambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        //使用MP相关方法查询
        List<Setmeal> list = setmealMapper.selectList(lambdaQueryWrapper);
        return list;
    }
    //添加套餐
    @CacheEvict(value = "setmeal",allEntries = true)
    @Override
    public void save(SetmealDto setmealDto) {
        int insert = setmealMapper.insert(setmealDto);
        if (insert!=1){
            throw new InsertException("添加数据时出现未知的异常");
        }
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //为SetmealDish类设置套餐id
        for (SetmealDish setmealDish :setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        //setmealDishes添加到数据库中
        setmealDishService.save(setmealDishes);
        //定义Redis中的key
        String key = "redis_"+ setmealDto.getCategoryId() + "_1";
        redisTemplate.delete(key);
    }

    @Override
    public Page<SetmealDto> page(Integer page, Integer pageSize, String name) {
        Page<SetmealDto> setmealPage = new Page<>(page,pageSize);
        List<SetmealDto> list = setmealMapper.list(setmealPage, name);
        setmealPage.setRecords(list);
        return setmealPage;
    }
    //删除套餐
    @CacheEvict(value = "setmeal",allEntries = true)
    @Override
    public void remove(List<Long> dis) {
        //只有停售的套餐才能删除，起售的不能删除所以首先要先判断一下
        //创建添加构造器，增加条件，根据条件进行查询
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<Setmeal>();
        lambdaQueryWrapper.in(dis!=null,Setmeal::getId,dis).eq(Setmeal::getStatus,1);
        //根据条件查询
        Integer count = setmealMapper.selectCount(lambdaQueryWrapper);
        if(count>0){
            throw new CountException("您删除的商品以起售无法删除");
        }
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.in(dis!=null,Setmeal::getId,dis);
        //删除套餐
        int i = setmealMapper.delete(lambdaQueryWrapper1);
        if (i<=0){
            throw  new DeleteException("删除数据时产生未知的异常");
        }
        //删除套餐中的菜品
        setmealDishService.remove(dis);

    }
    @Cacheable(value = "setmeal",key = "#setmeal.categoryId +'_'+#setmeal.status")
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        //根据套餐分类id查询出具体的套餐
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        lambdaQueryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        lambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> setmeals = setmealMapper.selectList(lambdaQueryWrapper);
        return setmeals;
    }
}
